package com.lavegue.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.lavegue.common.BaseHibernateDao;
import com.lavegue.common.Page;
import com.lavegue.dao.CompanyDao;
import com.lavegue.model.BisGroup;
import com.lavegue.model.Company;

public class CompanyDaoImpl extends BaseHibernateDao<Company, Integer> implements CompanyDao{

	public CompanyDaoImpl() {
		super(Company.class);
	}
	
	/**
	 * 根据公司ID查询公司名称
	 * @param 公司ID
	 * @return
	 */
	public List<Company> findByCompany() {
		String queryString = "from Company";
		List<Company>  cList = this.getHibernateTemplate().find(queryString);
		return cList;
	}
	/**
	 * 根据公司名称或者ID查询
	 * @return
	 */
	public Company findCompanyByName(String companyName,Integer companyId) {
		String queryString = "from Company c where c.companyName = ?";
		String queryString2 = "from Company c where c.id = ?";
		if(null != companyName && !companyName.equals("")){
			List list = getHibernateTemplate().find(queryString, companyName);
			if (!list.isEmpty()) {
				return (Company) list.get(0);
			}
		}else{
			List list2 = getHibernateTemplate().find(queryString2, companyId);
			if (!list2.isEmpty()) {
				return (Company) list2.get(0);
			}
		}
		
		return null;
	}

	//根据公司名称查询
    public Page<Company> getAllCompanyByName(int pageNo,int pageSize,String name)
    {
    	List<Criterion> condition = new ArrayList<Criterion>();
		if(null != name && !name.trim().equals("")){
		   condition.add(Restrictions.like("companyName", name, MatchMode.ANYWHERE));
		}
		Page<Company> page = this.findPage(pageNo, pageSize, condition, Order.desc("id"));
		return page;
    }
}
